/*  ----------------------------------------------------------------------------
*	This file is part of the AS3This ActionScript 3 library.
*	Author: Steve Jones for G-RAFF <http://www.g-raff.com/>
*	Copyright (c) 2009 raffWorks Ltd.
*	----------------------------------------------------------------------------
*	Please see the notice at the bottom of the code before using or modifying this class.
*/
package com.as3this.core
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;

	public class ServiceIcon extends Sprite
	{
		public static const BASE_URL		:String = "http://cache.addthiscdn.com/icons/v0/thumbs/<service-code>.gif";
		public static const DEFAULT_SIZE	:int = 16;
		
		public function ServiceIcon(x:Number, y:Number, serviceCode:String)
		{
			// disable mouse activity so it becomes part of the parent button
			this.x = x;
			this.y = y;
			this.mouseEnabled = false;
			
			var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadedHandler, false, 0, true);
			var req:URLRequest = new URLRequest(BASE_URL.replace("<service-code>", serviceCode));
			var context:LoaderContext = new LoaderContext(true);
			ldr.load(req, context);
			addChild(ldr);
		}
		
		protected function iconLoadedHandler(event:Event) : void
		{
			// can be overridden for custom behaviour
			var ldr:Loader = event.target.loader;
			
			if (ldr.width > DEFAULT_SIZE)
			{
				// smoothly does it
				var bmp:Bitmap = ldr.content as Bitmap;
				bmp.smoothing = true;
				ldr.width = ldr.height = DEFAULT_SIZE;
			}
		}
		
	}
}
/*
 *    AS3This is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    AS3This is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with AS3This.  If not, see <http://www.gnu.org/licenses/>.
 */